/**
 * 
 */
package cn.edu.scau.cmi.dengzhihao.dao;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import org.hibernate.Session;
import org.hibernate.Transaction;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class Text7Controller {

	@FXML
	private ResourceBundle resources;

	@FXML
	private URL location;

	@FXML
	private Button Button_addStudent;

	@FXML
	private Button Button_addTeacher;

	@FXML
	private TextArea database;

	@FXML
	private TextField s_id;

	@FXML
	private TextField s_name;

	@FXML
	private TextField s_tid;

	@FXML
	private TextField t_id;

	@FXML
	private TextField t_name;

	@FXML
	private Button Button_Update;

	@FXML
	private Button Button_DelectStudent;

	@FXML
	private Button Button_DelectTeacher;

	@FXML
	void a(ActionEvent event) {
	}

	@FXML
	void initialize() {
		assert Button_addStudent != null : "fx:id=\"Button_addStudent\" was not injected: check your FXML file 'Text7.fxml'.";
		assert Button_addTeacher != null : "fx:id=\"Button_addTeacher\" was not injected: check your FXML file 'Text7.fxml'.";
		assert database != null : "fx:id=\"database\" was not injected: check your FXML file 'Text7.fxml'.";
		assert s_id != null : "fx:id=\"s_id\" was not injected: check your FXML file 'Text7.fxml'.";
		assert s_name != null : "fx:id=\"s_name\" was not injected: check your FXML file 'Text7.fxml'.";
		assert s_tid != null : "fx:id=\"s_tid\" was not injected: check your FXML file 'Text7.fxml'.";
		assert t_id != null : "fx:id=\"t_id\" was not injected: check your FXML file 'Text7.fxml'.";
		assert t_name != null : "fx:id=\"t_name\" was not injected: check your FXML file 'Text7.fxml'.";

	}

	@FXML
	public void b() {
		System.out.println("b");
	}

	@FXML
	public void Button_addStudent() {
		Student s = new Student();
		s.setId(s_id.getText());
		s.setName(s_name.getText());
		int t_id = Integer.parseInt(s_tid.getText());
		TeacherDAO tDAO = new TeacherDAO();
		Teacher t = tDAO.findById(t_id);
		s.setTeacher(t);
		System.out.println("Student is ready!");
		Session session = HibernateSessionFactory.getSession();
		Transaction transaction = session.beginTransaction();
		System.out.println("transaction is ready!");
		StudentDAO sDAO = new StudentDAO();
		sDAO.save(s);
		System.out.println("Save!!");
		transaction.commit();
		session.close();
		System.out.println("Add Student!");
	}

	@FXML
	public void Button_addTeacher() {
		Teacher t = new Teacher();
		t.setId(Integer.parseInt(t_id.getText()));
		t.setName(t_name.getText());
		System.out.println("Teacher is ready!");
		Session session = HibernateSessionFactory.getSession();
		Transaction transaction = session.beginTransaction();
		System.out.println("transaction is ready!");
		TeacherDAO tDAO = new TeacherDAO();
		tDAO.save(t);
		System.out.println("Save!!");
		transaction.commit();
		session.close();
		System.out.println("Add teacher!");

	}

	@FXML
	public void Button_update() {
		StudentDAO sDAO = new StudentDAO();
		List<Student> students = sDAO.findAll();
		String message = new String();
		message = "student:";
		for (Student s : students) {
			String id = s.getId();
			String name = s.getName();
			int t_id = s.getTeacher().getId();
			message = message + "\n" + id + " " + name + " " + t_id;
		}
		TeacherDAO tDAO = new TeacherDAO();
		List<Teacher> teachers = tDAO.findAll();
		message = message + "\n" + "teacher:";
		for (Teacher t : teachers) {
			int id = t.getId();
			String name = t.getName();
			message = message + "\n" + id + " " + name;
		}
		database.setText(message);
		// HibernateSessionFactory.closeSession();
		System.out.println("Finshed!");
	}
	
	@FXML
	public void Button_DelectStudent(){
		StudentDAO sDAO=new StudentDAO();
		String Sid=s_id.getText();
		Student s=sDAO.findById(Sid);
		System.out.println("Find Student!");
		Session session=HibernateSessionFactory.getSession();
		Transaction transaction=session.beginTransaction();
		sDAO.delete(s);
		System.out.println("Delect Student!");
		transaction.commit();
		session.close();
	}
	
	@FXML
	public void Button_DelectTeacher(){
		TeacherDAO tDAO=new TeacherDAO();
		int Tid=Integer.parseInt(t_id.getText());
		Teacher t=tDAO.findById(Tid);
		System.out.println("Find Teacher!");
		Session session=HibernateSessionFactory.getSession();
		Transaction transaction=session.beginTransaction();
		tDAO.delete(t);
		System.out.println("Delect Teacher!");
		transaction.commit();
		session.close();
	}
	

}